Installation
Installing TypeScript
TypeScript compiler (tsc) is required to convert .ts files into .js.
Install globally (for all projects):
npm install -g typescript
- This installs the
tsc(TypeScript Compiler) command globally.
Check Installation
tsc -v
Compiling TypeScript Files Manually
Let’s say you have a file app.ts:
let message: string = "Hello, TypeScript!";
console.log(message);
Compile it to JavaScript:
tsc app.ts
This creates app.js:
var message = "Hello, TypeScript!";
console.log(message);
Then run it with Node:
node app.js
Problem: If you have multiple .ts files or want custom settings (like targeting ES6, strict mode, etc.), compiling manually each time becomes painful.
Solution: tsconfig.json
What is tsconfig.json
tsconfig.json is a configuration file that tells the TypeScript compiler how to compile your project.
You create it with:
tsc --init
This generates a file like:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true
}
}
Why use tsconfig.json
- Instead of typing
tsc file.tsagain and again, you just run:
tsc
and it compiles the whole project based on rules inside tsconfig.json.
-
Makes collaboration easier: All developers follow the same TypeScript rules.
-
Helps organize source files (
src/) and output files (dist/). -
Enforces strict checks (like type safety) automatically.
Integrate TypeScript with npm scripts
In package.json, add a scripts section like this:
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"build": "tsc",
"watch": "tsc --watch",
"start": "npm run build && node dist/index.js",
"dev": "ts-node-dev src/index.ts"
},
"devDependencies": {
"typescript": "^5.2.0",
"ts-node-dev": "^2.0.0"
}
}
"build": "tsc"→ Runs TypeScript compiler according to your tsconfig.json."watch": "tsc --watch"→ Watches for file changes and recompiles automatically.
Example Project Structure
my-project/
├── src/
│ ├── app.ts
│ └── utils.ts
├── dist/ (will be generated)
└── tsconfig.json
src/app.ts
import { add } from "./utils";
let result: number = add(5, 10);
console.log("Result:", result);
src/utils.ts
export function add(a: number, b: number): number {
return a + b;
}
tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true
}
}
Compile & Run
tsc # compiles everything in src/ to dist/
node dist/app.js
Key tsconfig.json Options
Here are some important ones:
-
target→ Specifies which version of JavaScript to compile into."ES5"→ older browsers"ES6"or"ES2015"→ modern JavaScript
-
module→ Defines how modules are compiled."commonjs"→ for Node.js"esnext"→ for modern browsers with ES modules
-
rootDir: Where your TypeScript source files are located. -
outDir: Where compiled JavaScript files will be stored. -
strict→ Turns on strict type-checking. It enables multiple rules like:
noImplicitAnystrictNullChecksstrictBindCallApply
-
esModuleInterop: Helps when importing CommonJS modules (likeexpress)."esModuleInterop": trueWithout this, you’d need:
import * as express from "express";With it, you can write:
import express from "express"; -
include&exclude- Control which files TS should compile."include": ["src/**/*"],
"exclude": ["node_modules", "dist"]include→ compile all files insrc/.exclude→ ignorenode_modules&dist/.
Setting Up TypeScript with Node.js
Initialization
-
Initialize a Node.js Project
mkdir ts-node-app
cd ts-node-app
npm init -y -
Install TypeScript
npm install typescript --save-dev -
Install Node.js type definitions
TypeScript needs type definitions for Node.js APIs:
npm install @types/node --save-dev -
Initialize TypeScript Configuration
npx tsc --initBasic
tsconfig.jsonfor Node.js:{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true
}
}
Adding Development Tools
-
ts-node: Run TypeScript directly without compiling manually.
npm install ts-node --save-dev -
nodemon: Automatically restarts Node server on code changes.
npm install nodemon --save-dev -
Add a script in
package.json:"scripts": {
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts"
}Now,
npm run devwill run your TypeScript Node.js server and reload on changes.
Running the Project
-
Compile manually (optional):
npx tsc
-
Compiles TypeScript from
src/todist/. -
Run compiled JS:
node dist/index.js
-
Or run directly with ts-node:
npm run dev